package com.rzcf.blink.pay.trigger.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * RocketMQ 接收消息
 *
 * @author Dongsf
 */
@Component
@Slf4j
public class MQListener {

    /**
     * 下游渠道订单同步，接收消息后系统下单处理
     * @param s com.rzcf.blink.zh.domain.downstream.model.valobj.OrderDetailSaveVO
     * @param channel
     * @param message com.rzcf.blink.zh.domain.downstream.model.valobj.OrderDetailSaveVO
     * @throws IOException
     */
    @RabbitListener(queues = MqConfigConstants.DOWNSTREAM_ORDER_SYNC_QUEUE, ackMode = "MANUAL")
    public void onMessage(@Payload String s, Channel channel, Message message) throws IOException {
        try {
            log.info("接收到RocketMQ消息 {}===={}", s, new String(message.getBody()));
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("处理消息失败，消息将被拒绝", e);
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }

}
